package com.ruoyi.web.controller.eleme;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.TaoBaoGoods;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.eleme.TaoBaoGoodsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @ClassName TaoBaoGoodsController
 * @Author 任宝坤
 * @Date 2025/9/8 16:42
 * @Version v1.0
 **/
@RestController
@RequestMapping("/tb")
public class TaoBaoGoodsController extends BaseController {

    @Resource
    private TaoBaoGoodsService taoBaoGoodsService;



    @GetMapping("/search")
    public TableDataInfo search(@RequestParam(value = "goodName", required = false) String goodName,
                                @RequestParam(value = "goodClass", required = false)String goodClass,
                                @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        startPage();
        List<TaoBaoGoods> list = taoBaoGoodsService.search(goodName, goodClass);
        return getDataTable(list);
    }

    @PostMapping("/import")
    public AjaxResult importGoods(@RequestParam("file") MultipartFile file){
        if (!file.getOriginalFilename().toLowerCase().endsWith("csv")){
            return error("文件格式不正确");
        }
        List<TaoBaoGoods> returnList = taoBaoGoodsService.importFile(file);
        try {
            if (!returnList.isEmpty()){
                taoBaoGoodsService.insertUpdate(returnList);
            }
        }catch (Exception e){
            return error("导入失败");
        }
        return success();
    }
}
